package com.iplus.ydkf.ehome.module.device;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.iplus.ydkf.ehome.App;
import com.iplus.ydkf.ehome.R;
import com.iplus.ydkf.ehome.common.net.Callback;
import com.iplus.ydkf.ehome.common.net.JsonParser;
import com.iplus.ydkf.ehome.common.net.NetClient;
import com.iplus.ydkf.ehome.config.ConString;
import com.iplus.ydkf.ehome.module.base.BaseFragment;
import com.iplus.ydkf.ehome.module.home.SensorActivity;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by liuhaitian on 16/5/8.
 */
public class DeviceFragment extends BaseFragment {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rv_room)
    RecyclerView recyclerView;
    SingleAdapter adapter;
    private static DeviceFragment fragment;

    private JSONObject roomObject = new JSONObject();

    public static DeviceFragment getInstance() {
        if (fragment == null)
            fragment = new DeviceFragment();
        return fragment;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_room;
    }

    @Override
    public void initView(View view) {
        toolbar.setVisibility(View.GONE);
        adapter = new SingleAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void initData(View view) {
    }

    @Override
    public String getFragmentName() {
        return "设备";
    }

    @Override
    protected void lazyLoad() {
        Map<String, String> header = new HashMap<>();
        header.put("userId", App.getCurrentUser().getObjectId());
        NetClient.sendGetRequest(ConString.CLOUD_DEVICE + "/getDevices", new Callback<JSONObject>(JsonParser.getInstance()) {
            @Override
            public void onResponse(JSONObject jsonObject) {
                if (jsonObject.getIntValue("errorCode") == 0) {
                    roomObject = jsonObject;
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(IOException e) {

            }
        }, header);
    }

    public class SingleAdapter extends RecyclerView.Adapter<SingleAdapter.ViewHolder> {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(), R.layout.item_device, null);
            view.setOnClickListener(new MyOnClickListener());
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            JSONObject room = roomObject.getJSONArray("data").getJSONObject(position);
            holder.tv_deviceName.setText(room.getString("cname"));
            holder.tv_deviceType.setText(room.getIntValue("type") == 0 ? "传感器" : "控制器");
        }

        @Override
        public int getItemCount() {
            return roomObject.containsKey("data") ? roomObject.getJSONArray("data").size() : 0;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv_item_device)
            TextView tv_deviceName;
            @Bind(R.id.tv_item_deviceType)
            TextView tv_deviceType;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

        public class MyOnClickListener implements View.OnClickListener {

            @Override
            public void onClick(View v) {
                int itemPosition = recyclerView.getChildLayoutPosition(v);
                JSONObject room = roomObject.getJSONArray("data").getJSONObject(itemPosition);
                Intent intent = new Intent(getActivity(), SensorActivity.class);
                intent.putExtra("type", room.getIntValue("type"));
                intent.putExtra("cname", room.getString("cname"));
                intent.putExtra("json", room.toJSONString());
                startActivity(intent);
            }
        }
    }
}